package com.hdyg.sixg.activity.home;

import android.os.Bundle;
import android.text.TextUtils;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.allen.library.SuperTextView;
import com.hdyg.sixg.R;
import com.hdyg.sixg.activity.common.BaseActivity;
import com.hdyg.sixg.entry.ScanBean;
import com.hdyg.sixg.mvp.Contrant.CScan;
import com.hdyg.sixg.mvp.base.BaseUrl;
import com.hdyg.sixg.mvp.base.GetParamUtil;
import com.hdyg.sixg.mvp.base.SpMsg;
import com.hdyg.sixg.mvp.presenter.PScan;
import com.hdyg.sixg.util.StringUtil;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;

import butterknife.BindView;

/**
 * @author CZB
 * @describe 扫一扫
 * @time 2019/4/17 14:50
 */
public class ScanActivity extends BaseActivity implements CScan.IVScan {

    @BindView(R.id.ll_top_left)
    LinearLayout llTopLeft;
    @BindView(R.id.tvTopTitle)
    TextView tvTopTitle;
    @BindView(R.id.et_money)
    EditText etMoney;
    @BindView(R.id.sv_fee)
    SuperTextView svFee;
    @BindView(R.id.sv_six)
    SuperTextView svSix;
    @BindView(R.id.et_pwd)
    EditText etPwd;
    @BindView(R.id.bt_sure)
    Button btSure;
    @BindView(R.id.srl_refresh)
    SmartRefreshLayout srlRefresh;

    private CScan.IPScan mPresenter;
    private String to_user_id, pay_amount, password;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_scan;
    }

    @Override
    protected void initView() {
        tvTopTitle.setText(R.string.scan_titlle);
        onClickLitener();
        to_user_id = getIntent().getStringExtra(SpMsg.INTENT_STR);
    }

    private void onClickLitener() {
        srlRefresh.setOnRefreshListener(refreshLayout -> {
            srlRefresh.finishRefresh(2000);//传入false表示刷新失败
            initData();
        });
        llTopLeft.setOnClickListener(v -> finish());
        //确认付款
        btSure.setOnClickListener(v -> {
            pay_amount = etMoney.getText().toString().trim();
            password = etPwd.getText().toString().trim();
            if (TextUtils.isEmpty(to_user_id)) {
                toastNotifyShort(R.string.toast_user_id);
                return;
            }
            if (TextUtils.isEmpty(pay_amount)) {
                toastNotifyShort(R.string.scan_money_hint);
                return;
            }
            if (TextUtils.isEmpty(password)) {
                toastNotifyShort(R.string.pay_pwd_hint);
                return;
            }
            if (StringUtil.isFastDoubleClick()){
                return;
            }
            mPresenter.pGetApplyPay(BaseUrl.SCAN_PAY_URL, GetParamUtil.getScanPayParam(to_user_id, pay_amount, password));

        });
    }

    @Override
    protected void initData() {
        mPresenter.pGetInit(BaseUrl.SCAN_INIT_URL, GetParamUtil.getScanInitParam());
    }

    @Override
    protected void createPresenter() {
        mPresenter = new PScan(this);
    }

    @Override
    public void vGetInitSuccess(ScanBean dataBean) {
        svFee.setRightString(dataBean.getFee());
        svSix.setRightString(dataBean.getSix_price());
    }

    @Override
    public void vGetApplyPaySuccess(ScanBean dataBean) {
        mBundle = new Bundle();
        mBundle.putString(SpMsg.INTENT_STR, dataBean.getSamount());
        startActivity(PaySuccessActivity.class, mBundle);
        finish();
    }
}
